import router from '~/router'
import store from '~/store'
import { createApp } from 'vue'
import App from './App.vue'
// import 'require/bin/require-command'
const app = createApp(App)
import { getUSerInfo } from "~/api/xm.js"
import { getToken } from '~/composables/auth'
import adminLayout from "~/layouts/admin.vue"
import userr from '~/pages/product/user.vue'
import huiyuan from '~/pages/product/huiyuan.vue'
import order from '~/pages/product/order.vue'
import product from '~/pages/product/product.vue'
import typee from '~/pages/product/type.vue'
import jinhuo from '~/pages/product/jinhuo.vue'
import tongji from '~/pages/product/tongji.vue'
import lirun from '~/pages/product/lirun.vue'
import tuihuo from '~/pages/product/tuihuo.vue'


// 是否已经加载了管理员信息
// 全局路由守卫
router.beforeEach((to, from, next) => {
        const token = getToken()
        console.log(token);
        if (token) {
            if (to.path === '/login') {
                next('/huiyuan');
            } else {
                let menus = store.state.menus;
                let user = store.state.user;
                console.log('menus', menus);
                if (menus.length && user.id) {
                    console.log("存在路由");
                    next();
                } else {
                    console.log("动态加载路由");
                    store.dispatch('getInfo', token).then(res => {
                        console.log("------------", res)
                        let routeList = []
                        // routeList = [{
                        //         path: '/huiyuan',
                        //         name: '会员管理',
                        //         component: huiyuan,
                        //         meta: {
                        //             title: '报销管理'
                        //         }
                        //     },
                        //     {
                        //         path: '/order',
                        //         name: '订单管理',
                        //         component: order,
                        //         meta: {
                        //             title: '报销管理'
                        //         }
                        //     },
                        //     {
                        //         path: '/product',
                        //         name: '商品管理',
                        //         component: product,
                        //         meta: {
                        //             title: '报销管理'
                        //         }
                        //     },
                        //     {
                        //         path: '/type',
                        //         name: '商品类别管理',
                        //         component: typee,
                        //         meta: {
                        //             title: '报销管理'
                        //         }
                        //     },
                        //     {
                        //         path: '/user',
                        //         name: '用户管理',
                        //         component: userr,
                        //         meta: {
                        //             title: '报销管理'
                        //         }
                        //     },
                        // ]
                        routeList = [
                            {
                                path: '/',
                                name: '首页',
                                component: tongji,
                                meta: {
                                    title: '报销管理'
                                }
                            },
                            {
                                name: '人员管理',
                                path: '/user',
                                children:[
                                    {
                                        path: '/huiyuan',
                                        name: '会员管理',
                                        component: huiyuan,
                                        meta: {
                                            title: '报销管理'
                                        }
                                    },   {
                                        path: '/user',
                                        name: '用户管理',
                                        component: userr,
                                        meta: {
                                            title: '报销管理'
                                        }
                                    },
                                ]
                            },
                            {
                                
                                name: '库存管理',
                                path: '/kucun',
                                children:[
                                    {
                                        path: '/product',
                                        name: '商品管理',
                                        component: product,
                                        meta: {
                                            title: '报销管理'
                                        }
                                    },
                                    {
                                        path: '/type',
                                        name: '商品类别管理',
                                        component: typee,
                                        meta: {
                                            title: '报销管理'
                                        }
                                    },
                                ]
                            },
                         
                          
                        {
                            path: '/order',
                            name: '订单管理',
                            component: order,
                            meta: {
                                title: '报销管理'
                            }
                        },
                      
                        {
                            path: '/jinhuo',
                            name: '进货管理',
                            component: jinhuo,
                            meta: {
                                title: '报销管理'
                            }
                        }
                     
                    ]
                        store.commit('SET_MENUS', routeList)
                        router.addRoute({
                            path: "/",
                            component: adminLayout,
                            children: routeList
                        })
                        router.push({...to, replace: true })
                    });
                }
            }
        } else {
            console.log(to.path);
            if (to.path === '/login') {
                console.log('登录页面');
                next();
            } else {
                next('/login');
            }
        }

    })
    // 设置页面标题
    // let title = (to.meta.title ? to.meta.title : "")
    // document.title = title
    // router.afterEach(() => hideFullLoading())
    // let routeList = []
    // if (res == 0) {
    //     console.log("学生");
    //     routeList = [{
    //             path: '/chakanZuoye',
    //             name: '查看作业',
    //             component: chakanZuoye,
    //             meta: {
    //                 title: '查看作业'
    //             }
    //         },
    //         {
    //             path: '/chakanChengji',
    //             name: '查看成绩',
    //             component: chakanChengji,
    //             meta: {
    //                 title: '查看成绩'
    //             }
    //         }
    //     ]
    // } else {
    //     console.log("老师");
    //     routeList = [{
    //             path: '/chakanBanji',
    //             name: '查看班级',
    //             component: chakanBanji,
    //             meta: {
    //                 title: '查看班级'
    //             }
    //         },
    //         {
    //             path: '/pigaiZuoye',
    //             name: '批改作业',
    //             component: pigaiZuoye,
    //             meta: {
    //                 title: '批改作业'
    //             }
    //         },
    //         {
    //             path: '/xueshengg',
    //             name: '学生信息',
    //             component: xueshengg,
    //             meta: {
    //                 title: '学生信息'
    //             }
    //         }
    //     ]
    // }
    // console.log("routeList", routeList);
    // store.commit('SET_MENUS', routeList)
    // router.addRoute({
    //         path: "/",
    //         component: adminLayout,
    //         children: routeList
    //     })
    // router.push({...to, replace: true })